﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QFramework;
using System;
using System.IO;
using System.Reflection;

namespace HareGame
{
    [MonoSingletonPath("GameEntry/ConfigManager")]
    public class ConfigManager : MonoBehaviour, ISingleton
    {
        private Dictionary<Type, object> m_Configs;
        /// <summary>
        /// 初始化
        /// </summary>
        public void OnSingletonInit()
        {
            m_Configs = new Dictionary<Type, object>();
            Config config = new Config();
            FieldInfo[] infos = config.GetType().GetFields();
            foreach (var info in infos)
            {
                Type type = info.FieldType;
                try
                {
                    object obj = GameEntry.Util.ReadFromStreamAssets("Configs/" + type.Name + ".json", type);
                    m_Configs.Add(type, obj);
                }
                catch (Exception e)
                {
                    LogKit.E("配置文件" + type.Name + "加载失败:" + e.Message);
                }
            }
        }

        /// <summary>
        /// 定义单例
        /// </summary>
        public static ConfigManager Instance
        {
            get { return MonoSingletonProperty<ConfigManager>.Instance; }
        }

        public T GetConfig<T>()
        {
            T t = default(T);
            Type type = typeof(T);
            foreach (var v in m_Configs)
            {
                if (v.Key == type)
                {
                    t = (T)v.Value;
                }
            }
            return t;
        }
    }
}